/**
 * 存储服务核心类型
 * 定义LocalStorage适配器需要遵循的接口和返回结构
 */

import type { Content, ContentType } from '../../types/document'

/**
 * 最近访问的内容条目
 */
export interface RecentContentEntry {
  id: string
  title: string
  type: ContentType
  savedAt: string
}

/**
 * 内容列表项
 * 用于文档列表展示
 */
export interface ContentListItem {
  id: string
  title: string
  type: ContentType
  status: string
  createdAt: Date
  updatedAt: Date
  wordCount: number
  preview?: string
}

/**
 * 内容查询参数
 */
export interface ContentQuery {
  type?: ContentType[]
  sortBy?: 'createdAt' | 'updatedAt' | 'title'
  sortOrder?: 'asc' | 'desc'
  limit?: number
}

/**
 * 存储操作的统一返回结果
 */
export type StorageResult<T> =
  | { ok: true; data: T }
  | { ok: false; error: StorageError }

/**
 * 存储适配器接口
 */
export interface StorageAdapter {
  saveContent(content: Content): StorageResult<void>
  getContentById(id: string): StorageResult<Content | null>
  removeContent(id: string): StorageResult<void>
  listRecent(limit?: number): StorageResult<RecentContentEntry[]>
  getContents(query?: ContentQuery): StorageResult<ContentListItem[]>
  clear(): StorageResult<void>
  getNamespaceUsage(): StorageResult<StorageUsageSummary>
}

/**
 * 存储空间占用信息
 */
export interface StorageUsageSummary {
  itemCount: number
  usedBytes: number
}

/**
 * 存储错误码
 */
export enum StorageErrorCode {
  UNAVAILABLE = 'unavailable',
  SERIALIZATION_FAILED = 'serialize_failed',
  DESERIALIZATION_FAILED = 'deserialize_failed',
  QUOTA_EXCEEDED = 'quota_exceeded',
  UNKNOWN = 'unknown'
}

/**
 * 存储错误结构
 */
export interface StorageError {
  code: StorageErrorCode
  message: string
  cause?: unknown
}
